--[[--
- @Copyright (C), 2016-, sixiong.
- @Name index.lua
- @Author sixiong
- @Version 1.0
- @Date: 2016年6月11日下午22:06:41
- @Description 首页控制器
- @Class
- @Function List
- @History <author> <time> <version > <desc>
            sixiong 2016年6月11日下午22:06:41  1.0  第一次建立该文件
--]]
local cjson = require "cjson";
local index = controller.new();

index.category = {};

--初始化动作
function index:init()
  
        self:_category(); -- 初始化栏目
        self:_getrecom();-- 推荐文章
end
  
  
-- 推荐文章
function index:_getrecom()
           local res = {};
           local m = model.__get("article"); 
           res = m:getrecom({recom = 1}, '0,5');
           self.assign('recom', res);
end

--获取栏目--
function index:_category()
          local m = model.__get("category"); 
          local menu = m:getall();   
                 --dump(menu);
          local option = {};
	       
	      local u = nil;
	       if menu then 
	             for k, v in pairs(menu) do
	                 u = v['id'];
	                option[u] = v;
	             end
	       end
        
           index.category = option;   
		     -- 右侧栏目start----  
	       local oneTree = {}; --顶级栏目 
	       local i = 1; 
		   for k, v in pairs(menu) do
		        if  v['parent_id'] == 0 then
		            oneTree[i] = v; 
		            i = i +1;
		        end 
		   end
		     
	       local n = 1;  
		   for k, v in pairs(oneTree) do
	            n = 1;
	            for key, val in pairs(menu) do
			            if  val['parent_id'] == v['id'] then 
					          if not oneTree[k]['child'] then 
					                oneTree[k]['child'] = {}; 
					            end 
					            				                 
			                   oneTree[k]['child'][n]  = val;
			                   n = n +1;
			             end 
	            end 
		   end
          
          self.assign('oneTree', oneTree);
         -- 右侧栏目end----  
end  
  
  
  
  
  
  
  
--[[
- @desc   首页
- @param  nil
- return  void
--]]
function index:index() 


       _GET  = ngx.req.get_uri_args();
       local page =  vendor("page");  --加载分页库 .
       local m = model.__get("article");--加载模型 .
      
       local countNum = tonumber(m:count({}));  --总条数
       local res ={};  
        local  pageres ={}
        if countNum > 0 then 
           pageres = page:get(countNum, 15, _GET['page']); 
           res = m:getall({}, pageres['limit']);  
        end 
         
       local option = (index.category);  --栏目table
      
        if res then
         for key,val in pairs(res) do           --文章table 
             local cate_id = tonumber(val['category_id']);   --栏目id
             if val['category_id'] and option[cate_id]  then
                 val['category_id'] = option[cate_id]['typename']; 
             end 
             
             res[key] = val;  
         end
       end
        
        self.assign('list', res);
        self.assign('page', pageres);
        self.assign('title', "首页"); 
        self.render('zh_cn/index/main.html'); 
end 
 
 
 
 
 
--[[
- @desc   文章页
- @param  nil
- return  void
--]]
function index:article()
       _GET  = ngx.req.get_uri_args();
       local id = _GET['id'];
       local res ={};  
        
       if id  ~= nil then
            id = tonumber(id);
       end
        
       if id < 1 then 
          self.redirect('/index/index');
       end
        
	   local m = model.__get("article");--加载模型 . 
	         res = m:getone(id);      --mysql拿文章
            
       if not res then 
             self.redirect('/index/index');
       end 
        
       self.assign('list', res);
       self.assign('title', "文章内容页面");
       self.render('zh_cn/index/article.html'); 
end 
 
 
 
 
 --[[
- @desc  栏目列表页
- @param  nil
- return  void
--]]
function index:type()
        _GET  = ngx.req.get_uri_args();
        local id = _GET['id'];
        local res ={};  
        
        if id  ~= nil then
            id = tonumber(id);
        end
  
        local option = {};  
    
        --先看这个id是否有效  
        option[1] = (index.category)[id]['id']; 
        if not option[1] then
             self.redirect('/index/index');
        end
          
       --如果有子目录
       local i = 2;
       for key, val in pairs(index.category) do   
             if  (val['parent_id'] == option[1])  then
	              option[i] = val['id']; 
	              i = i +1;
             end 
       end    
    
       local page =  vendor("page");  --加载分页库 .
       local m = model.__get("article");--加载模型 . 
       local countNum = tonumber(m:count({category_id = option}));  --总条数
   
   
       local  pageres ={};
       if countNum > 0 then
           pageres = page:get(countNum, 15, _GET['page']); 
           res = m:getall({category_id = option}, pageres['limit']);  
       end 
        
         
       if res then
          for key,val in pairs(res) do           --文章table 
             local cate_id = tonumber(val['category_id']);   --栏目id
   
             if val['category_id'] and (index.category)[cate_id]  then 
                 val['category_id'] = (index.category)[cate_id]['typename']; 
             end 
             
             res[key] = val;  
          end
       end
         
       self.assign('list', res);
       self.assign('page', pageres);
       self.assign('title', "iepush列表"); 
       self.render('zh_cn/index/main.html');  
end 
   
   
--[[
- @desc   新随笔
- @param  nil
- return  void
--]]
function index:say() 
        local page =  vendor("page");  --加载分页库 .
       local m = model.__get("article");--加载模型 . 
       
       local option = {};
       option[1] = 29;
       
       local countNum = tonumber(m:count({category_id = option}));  --总条数
       
       local res = {};
       local  pageres ={};
       if countNum > 0 then
           pageres = page:get(countNum, 15);
           res = m:getall({category_id = option},  pageres['limit']);  
        end 
        
        self.assign('list', res);
        self.assign('page', pageres); 
        self.assign('title', "新随笔");  
        self.render('zh_cn/index/main.html'); 
end  
 
 
--[[
- @desc   关于我
- @param  nil
- return  void
--]]
function index:about() 
       local id = 45;
        local res ={};   
           --获取文章--
        local cache = model.__get("cache");   
        local res,a,b= cache:get( 'artcle__result__key'.. id );  --去redis取

        if  type(res) == "userdata" then  -- type类型
	        local m = model.__get("article");--加载模型 . 
	         res = m:getone(id);      --mysql拿文章
             cache:set('artcle__result__key'.. id,  tostring(cjson.encode(res)));    --存redis
        else
              res = cjson.decode(res); 
        end  
           
        self.assign('list', res);
        self.assign('title', "关于我");
        self.render('zh_cn/index/article.html'); 
end  
   
return index;


